﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HoldNote : MonoBehaviour
{
    public float noteSpeed;
    public KeyCode keyToPress;
    private bool keyPressed = false;
    private bool headJudged = false;//是否已经判定了长条头
    public GameObject pureEffect;
    public GameObject farEffect;
    public GameObject lostEffect;
    public GameObject hitPureEffect;
    public GameObject hitFarEffect;
    public GameObject hitLostEffect;
    public GameObject k1, k2, k3, k4; //传入4个键位 为了获取其位置赋给hit效果
    private GameObject hitEffect;//保存实例化后的hit效果
    private GameObject hitEffect2;//保存实例化后的打击特效
    private Vector3 effectPos; //hit效果位置的下方按键位置
    private Vector3 startLocalScale; //hold note触发头判时的localscale
    private float noteScale; //note适应分辨率缩放比例
    private int i = 1; //每半拍一次判定的计数变量
    public int holdCombo;//此hold note除了头判以外的物量
    private float startTime;//hold note触发头判时的Time.time
    
    // Start is called before the first frame update
    void Start()
    {
        noteScale = (GlobalSettings.Instance.standardWidth / GlobalSettings.Instance.standardHeight) / ((float)Screen.width / (float)Screen.height);
        transform.localScale = transform.localScale / noteScale;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position -= new Vector3(0f, noteSpeed * Time.deltaTime, 0f);

        if (transform.position.y < GlobalSettings.Instance.k1Pos.y + 0.12f * noteSpeed)
        {
            if (keyToPress == GlobalSettings.Instance.key1)
            {
                GlobalSettings.Instance.k1InZone = true;
                if (GlobalSettings.Instance.k1Pressed == true)
                {
                    keyPressed = true;
                    effectPos = k1.GetComponent<RectTransform>().anchoredPosition;
                    GlobalSettings.Instance.k1Pressed = false;
                    GlobalSettings.Instance.k1InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k1InZone = missLost();
                }
            }
            else if (keyToPress == GlobalSettings.Instance.key2)
            {
                GlobalSettings.Instance.k2InZone = true;
                if (GlobalSettings.Instance.k2Pressed == true)
                {
                    keyPressed = true;
                    effectPos = k2.GetComponent<RectTransform>().anchoredPosition;
                    GlobalSettings.Instance.k2Pressed = false;
                    GlobalSettings.Instance.k2InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k2InZone = missLost();
                }
            }
            else if (keyToPress == GlobalSettings.Instance.key3)
            {
                GlobalSettings.Instance.k3InZone = true;
                if (GlobalSettings.Instance.k3Pressed == true)
                {
                    keyPressed = true;
                    effectPos = k3.GetComponent<RectTransform>().anchoredPosition;
                    GlobalSettings.Instance.k3Pressed = false;
                    GlobalSettings.Instance.k3InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k3InZone = missLost();
                }
            }
            else if (keyToPress == GlobalSettings.Instance.key4)
            {
                GlobalSettings.Instance.k4InZone = true;
                if (GlobalSettings.Instance.k4Pressed == true)
                {
                    keyPressed = true;
                    effectPos = k4.GetComponent<RectTransform>().anchoredPosition;
                    GlobalSettings.Instance.k4Pressed = false;
                    GlobalSettings.Instance.k4InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k4InZone = missLost();
                }
            }
            if (keyPressed == true && headJudged == false)
            {
                //if (Mathf.Abs(transform.position.y - GlobalSettings.Instance.k1Pos.y) > 0.1f * noteSpeed)
                //{
                //    GameManager.Instance.lostHit();
                //    GlobalSettings.Instance.lostNum += holdCombo;
                //    keyPressed = false;
                //    hitEffect = Instantiate(lostEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                //    hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 40f, 0f);
                //    headJudged = false;
                //    i = 1;
                //    notePool.Instance.RecycleObj(this.gameObject);
                //}
                //else if (Mathf.Abs(transform.position.y - GlobalSettings.Instance.k1Pos.y) > 0.05f * noteSpeed)
                if (Mathf.Abs(transform.position.y - GlobalSettings.Instance.k1Pos.y) > 0.05f * noteSpeed)
                {
                    GameManager.Instance.farHit();
                    keyPressed = false;
                    hitEffect = Instantiate(farEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                    hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 80f, 0f);
                    hitEffect2 = Instantiate(hitFarEffect);
                    hitEffect2.transform.position = getKeyPos();
                    headJudged = true;
                    startLocalScale = transform.Find("HoldTail").transform.localScale;
                }
                else
                {
                    GameManager.Instance.pureHit();
                    keyPressed = false;
                    hitEffect = Instantiate(pureEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                    hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 80f, 0f);
                    hitEffect2 = Instantiate(hitPureEffect);
                    hitEffect2.transform.position = getKeyPos();
                    headJudged = true;
                    startLocalScale = transform.Find("HoldTail").transform.localScale;
                }
            }
#if UNITY_ANDROID
            if (isHolding() && headJudged == true)
#else
            if (Input.GetKey(keyToPress) && headJudged == true)
#endif
            {
                transform.position = new Vector3(transform.position.x, GlobalSettings.Instance.k1Pos.y,transform.position.z);
                transform.Find("HoldTail").transform.localScale -= new Vector3(0f, 2f * noteSpeed * noteScale * Time.deltaTime, 0f);
                Vector3 TailPos = transform.Find("HoldTail").transform.localPosition;
                Vector3 TailScale = transform.Find("HoldTail").transform.localScale;
                transform.Find("HoldTail").transform.localPosition = new Vector3(TailPos.x, TailScale.y / 4f, TailPos.z);
                if (transform.Find("HoldTail").transform.localScale.y < startLocalScale.y - 2f * i * GlobalSettings.Instance.streamMultiply * noteScale)
                {
                    holdCombo--;
                    if (holdCombo < 0)
                    {
                        holdCombo = 0;
                    }
                    else
                    {
                        GameManager.Instance.pureHit();
                        hitEffect = Instantiate(pureEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                        hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 80f, 0f);
                        hitEffect2 = Instantiate(hitPureEffect);
                        hitEffect2.transform.position = getKeyPos();
                        i++;
                    }
                }

                if (TailScale.y<=0)
                {
                    keyPressed = false;
                    headJudged = false;
                    if(keyToPress == GlobalSettings.Instance.key1) //之所以这里要设false主要是由于holdnote的keydown只触发一次但是长按的过程中一直会让inzone变为true
                    {
                        GlobalSettings.Instance.k1InZone = false;
                    }
                    else if(keyToPress == GlobalSettings.Instance.key2)
                    {
                        GlobalSettings.Instance.k2InZone = false;
                    }
                    else if (keyToPress == GlobalSettings.Instance.key3)
                    {
                        GlobalSettings.Instance.k3InZone = false;
                    }
                    else
                    {
                        GlobalSettings.Instance.k4InZone = false;
                    }
                    i = 1;
                    notePool.Instance.RecycleObj(this.gameObject);
                }

            }
        }
    }
    private bool missLost() //将漏掉导致的lost的判定以及回收音符写进函数里 方便赋值给对应的keyinzone
    {
        if (transform.position.y < GlobalSettings.Instance.k1Pos.y - 0.1f * noteSpeed)
        {
            if(headJudged==true) //如果已经有过头判则不调用会将lostnum++的lostHit()
            {
                if(holdCombo != 0)
                {
                    GlobalSettings.Instance.combo = 0;
                    GameObject.Find("EffectCanvas/Combo").GetComponent<Text>().text = "";
                }
                
            }
            else
            {
                GameManager.Instance.lostHit();
            }
            if (holdCombo != 0)
            {
                GlobalSettings.Instance.lostNum += holdCombo;
                hitEffect = Instantiate(lostEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                hitEffect.GetComponent<RectTransform>().anchoredPosition = getEffectPos() + new Vector3(0f, 80f, 0f);
            }
                
            keyPressed = false;
            headJudged = false;
            i = 1;
            notePool.Instance.RecycleObj(this.gameObject);
            return false;
        }
        else
        {
            return true;
        }
    }
#if UNITY_ANDROID
    private bool isHolding()
    {
        if(keyToPress == GlobalSettings.Instance.key1)
        {
            return GlobalSettings.Instance.k1Holding;
        }
        else if(keyToPress == GlobalSettings.Instance.key2)
        {
            return GlobalSettings.Instance.k2Holding;
        }
        else if(keyToPress == GlobalSettings.Instance.key3)
        {
            return GlobalSettings.Instance.k3Holding;
        }
        else
        {
            return GlobalSettings.Instance.k4Holding;
        }
    }
#endif

    private Vector3 getKeyPos()
    {
        if (keyToPress == GlobalSettings.Instance.key1)
        {
            return GlobalSettings.Instance.k1Pos;
        }
        else if (keyToPress == GlobalSettings.Instance.key2)
        {
            return GlobalSettings.Instance.k2Pos;
        }
        else if (keyToPress == GlobalSettings.Instance.key3)
        {
            return GlobalSettings.Instance.k3Pos;
        }
        else
        {
            return GlobalSettings.Instance.k4Pos;
        }
    }

    private Vector3 getEffectPos()
    {
        if (keyToPress == GlobalSettings.Instance.key1)
        {
            return k1.GetComponent<RectTransform>().anchoredPosition;
        }
        else if (keyToPress == GlobalSettings.Instance.key2)
        {
            return k2.GetComponent<RectTransform>().anchoredPosition;
        }
        else if (keyToPress == GlobalSettings.Instance.key3)
        {
            return k3.GetComponent<RectTransform>().anchoredPosition;
        }
        else
        {
            return k4.GetComponent<RectTransform>().anchoredPosition;
        }
    }
}





